package cn.zbmao.web.util.mail;

import org.apache.commons.mail.*;

import java.net.MalformedURLException;
import java.net.URL;

public class SendMail {
//    发用邮件邮箱
	private static final String USER_NAME = "zhaobiao@zbmao.cn";
	private static final String USER_PASSWORD = "2wsx_1qaz";
	private static final String HOST_NAME = "smtp.zbmao.cn";
	private static final int SMTP_PORT = 25;


	public void getSimpleEmail(String msg, String toMail,String subject) {
		SimpleEmail email;
		try {
			email = (SimpleEmail) getEmail("SimpleEmail");
			email.setMsg(msg);
			email.setSubject(subject);
			email.addTo(toMail,toMail);
			email.send();// 发送
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
	//we must have change.
	@SuppressWarnings("unused")
	private void sendSimpleEmail(String msg, String toMail)
			throws EmailException {
		SimpleEmail email = (SimpleEmail) getEmail("SimpleEmail");
		email.setMsg(msg);
		email.addTo(toMail);
		email.send();// 发送
	}

	@SuppressWarnings("unused")
	private void sendMultiPartEmail() throws EmailException,
			MalformedURLException {
		EmailAttachment attachment = new EmailAttachment();
		// attachment.setPath("mypictures/john.jpg");
		attachment.setURL(new URL(
				"http://www.apache.org/images/asf_logo_wide.gif"));
		attachment.setDisposition(EmailAttachment.ATTACHMENT);
		attachment.setDescription("Picture of John");
		attachment.setName("John");

		// Create the email message
		MultiPartEmail email = (MultiPartEmail) getEmail("MultiPartEmail");

		// add the attachment
		email.attach(attachment);

		// send the email
		email.send();
	}

	
	public void sendHtmlEmail( String toMail,String htmlMsg,String subject) throws EmailException, MalformedURLException {
		// Create the email message
		HtmlEmail email = (HtmlEmail) getEmail("HtmlEmail");
		
		//add toMail
		email.addTo(toMail);
		email.setHtmlMsg(htmlMsg);
		email.setSubject(subject);
		
//		// embed the image and get the content id
//		URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
//		String cid = email.embed(url, "Apache logo");
//
//		// set the html message
//		email.setHtmlMsg("<html>The apache logo - <img src=\"cid:" + cid
//				+ "\"></html>");

		// set the alternative message
		email.setTextMsg("Your email client does not support HTML messages");

		// send the email
//		email.send();
		email.send();
	}

    /**
     * 判断发送的时什么格式邮件
     * @param etype
     * @return
     * @throws EmailException
     */
	@SuppressWarnings("unused")
	private Email getEmail(String etype) throws EmailException {
		Email email = null;
		if ("HtmlEmail".equals(etype)) {
			email = new HtmlEmail();
			email.setAuthentication(USER_NAME, USER_PASSWORD);
			email.setHostName(HOST_NAME);
//			 email.addTo("422271406@qq.com", "兔子");// 收信者
			email.setFrom(USER_NAME, "招标猫邮件系统");// 发信者
			email.setSmtpPort(SMTP_PORT);
			email.setSocketTimeout(10000);
//			email.setSubject("Test email with inline image");
		} else if ("MultiPartEmail".equals(etype)) {
			email = new MultiPartEmail();
			email.setHostName(HOST_NAME);
			email.setAuthentication(USER_NAME, USER_PASSWORD);
			// email.addTo("422271406@qq.com", "兔子");// 收信者
			email.setFrom(USER_NAME, "招标猫邮件系统");// 发信者
			email.setSubject("The picture");
			email.setMsg("Here is the picture you wanted");
		} else {
			email = new SimpleEmail();
			email.setHostName(HOST_NAME);// 邮件服务器
			email.setAuthentication(USER_NAME, USER_PASSWORD);// smtp认证的用户名和密码
			// email.addTo("422271406@qq.com", "");// 收信者
			email.setFrom(USER_NAME, "招标猫邮件系统");// 发信者
//			email.setSubject("招标邮件系统的测试邮件");// 标题
//			email.setCharset("UTF-8");// 编码格式
			// email.setMsg("这是一封猫咪的测试邮件");// 内容
		}

		return email;
	}
}